package com.mvc.business.service;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mvc.ProjectUtils;
import com.mvc.business.model.OrderAssociated;
import com.mvc.finance.service.BankStatementItemManager;
import com.mvc.finance.service.CommercialInvoiceItemManager;
import com.mvc.framework.service.BaseService;

@Service
public class OrderAssociatedManager extends BaseService<OrderAssociated, Integer> {

	@Autowired
	private PackingItemManager	         packingItemManager;

	@Autowired
	private CommercialInvoiceItemManager	commercialInvoiceItemManager;

	@Autowired
	private BankStatementItemManager	 bankStatementItemManager;

	public List<OrderAssociated> getPackingOrders(Integer packingId) {
		return searchBySql("select A from " + OrderAssociated.class.getName() + " A where associatedId=? and type=?", new Object[] {
		        packingId, OrderAssociated.TYPE_PACKING });
	}

	public List<OrderAssociated> getCommercialInvoiceOrders(Integer orderId) {
		return searchBySql("select A from " + OrderAssociated.class.getName() + " A where associatedId=? and type=?", new Object[] {
		        orderId, OrderAssociated.TYPE_COMMERCIAL_INVOICE });
	}

	public List<OrderAssociated> getBankStatementOrders(Integer bankStatementId) {
		return searchBySql("select A from " + OrderAssociated.class.getName() + " A where associatedId=? and type=?", new Object[] {
		        bankStatementId, OrderAssociated.TYPE_BANK_STATEMENT });
	}

	public void updateAssociated(Integer oldOrderId, Integer newOrderId, Integer type) {
		Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Query query = session.createQuery("update " + OrderAssociated.class.getName() + " set associatedId = " + newOrderId
		        + " where associatedId = " + oldOrderId + " AND type=" + type);
		query.executeUpdate();
	}

	public String getPackingOrderStr(Integer packingId) {
		List<OrderAssociated> associateds = getPackingOrders(packingId);
		if (associateds.size() > 0) {
			StringBuilder result = new StringBuilder();
			for (OrderAssociated associated : associateds) {
				result.append(ProjectUtils.getOrderNo(associated.getOrderId())).append(", ");
			}
			return result.substring(0, result.length() - 2);
		}
		return "";
	}

	public String getCommercialInvoiceOrderStr(Integer commercialInvoiceId) {
		List<OrderAssociated> associateds = getCommercialInvoiceOrders(commercialInvoiceId);
		if (associateds.size() > 0) {
			StringBuilder result = new StringBuilder();
			for (OrderAssociated associated : associateds) {
				result.append(ProjectUtils.getOrderNo(associated.getOrderId())).append(", ");
			}
			return result.substring(0, result.length() - 2);
		}
		return "";
	}

	public String getBankStatementPurchaseStr(Integer bankStatementId) {
		List<OrderAssociated> associateds = getBankStatementOrders(bankStatementId);
		if (associateds.size() > 0) {
			StringBuilder result = new StringBuilder();
			for (OrderAssociated associated : associateds) {
				result.append(ProjectUtils.getPurchaseNo(associated.getOrderId())).append(", ");
			}
			return result.substring(0, result.length() - 2);
		}
		return "";
	}

	public void savePackingOrders(Integer packingId) {
		List<Integer> orderIds = packingItemManager.getOrderIds(packingId);
		deleteByWhere("associatedId=" + packingId + " and type=" + OrderAssociated.TYPE_PACKING);
		for (Integer orderId : orderIds) {
			OrderAssociated associated = new OrderAssociated();
			associated.setAssociatedId(packingId);
			associated.setOrderId(orderId);
			associated.setType(OrderAssociated.TYPE_PACKING);
			save(associated);
		}
	}

	public void saveCommercialInvoiceOrders(Integer commercialInvoiceId) {
		List<Integer> orderIds = commercialInvoiceItemManager.getOrderIds(commercialInvoiceId);
		deleteByWhere("associatedId=" + commercialInvoiceId + " and type=" + OrderAssociated.TYPE_COMMERCIAL_INVOICE);
		for (Integer orderId : orderIds) {
			OrderAssociated associated = new OrderAssociated();
			associated.setAssociatedId(commercialInvoiceId);
			associated.setOrderId(orderId);
			associated.setType(OrderAssociated.TYPE_COMMERCIAL_INVOICE);
			save(associated);
		}
	}

	public void saveBankStatementPurchases(Integer bankStatementId) {
		List<Integer> orderIds = bankStatementItemManager.getPurchaseIds(bankStatementId);
		deleteByWhere("associatedId=" + bankStatementId + " and type=" + OrderAssociated.TYPE_BANK_STATEMENT);
		for (Integer orderId : orderIds) {
			OrderAssociated associated = new OrderAssociated();
			associated.setAssociatedId(bankStatementId);
			associated.setOrderId(orderId);
			associated.setType(OrderAssociated.TYPE_BANK_STATEMENT);
			save(associated);
		}
	}
}
